﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace V3MainTool.Model
{
    /// <summary>
    /// 生产方式
    /// </summary>
    public class ProductionMethodFamily : IParadoxRead, IParadoxWrite
    {
        public IList<ProductionMethod> MyList { get; set; }
        public ProductionMethodFamily()
        {
            MyList = new List<ProductionMethod>();
        }

        public void TokenCallback(ParadoxParser parser, string token)
        {
            ProductionMethod item = parser.Parse(new ProductionMethod());
            item.name = token;
            MyList.Add(item);
        }

        public void Write(ParadoxStreamWriter writer)
        {
            for (int i = 0; i < MyList.Count; i++)
            {
                writer.Write(MyList[i].name, MyList[i]);
            }
        }
    }
    public class ProductionMethod : IParadoxRead, IParadoxWrite
    {
        #region 转换
        public string nameStr
        {
            get
            {
                Dictionary<string, string> dic = LocalHelper.ProductionMethod;
                if (dic.TryGetValue(name, out string val))
                {
                    return val;
                }
                return name;
            }
        }
        public string unlocking_technologiesStr
        {
            get
            {
                if (unlocking_technologies == null)
                {
                    return "";
                }
                if (unlocking_technologies.Count == 0)
                {
                    return "";
                }
                Dictionary<string, string> dic = LocalHelper.InventionsDic;
                string str = "";
                foreach (var item in unlocking_technologies)
                {
                    if (dic.TryGetValue(item, out string val))
                    {
                        str += val + ",";
                    }
                    else
                    {
                        str += item + ",";
                    }
                }

                return str.Substring(0, str.Length - 1);
            }
        }
        public string unlocking_production_methodsStr
        {
            get
            {
                if (unlocking_production_methods == null)
                {
                    return "";
                }
                if (unlocking_production_methods.Count == 0)
                {
                    return "";
                }
                Dictionary<string, string> dic = LocalHelper.ProductionMethod;
                string str = "";
                foreach (var item in unlocking_production_methods)
                {
                    if (dic.TryGetValue(item, out string val))
                    {
                        str += val + ",";
                    }
                    else
                    {
                        str += item + ",";
                    }
                }

                return str.Substring(0, str.Length - 1);
            }
        }
        #endregion
        public string name { get; set; }
        public string texture { get; set; }
        public string is_default { get; set; }
        public float? ai_value{ get; set; }
        /// <summary>
        /// 污染
        /// </summary>
        public int? pollution_generation { get; set; }
        public IList<string> unlocking_technologies { get; set; }
        public IList<string> unlocking_production_methods { get; set; }
        public IList<string> disallowing_laws { get; set; }
        public IList<string> unlocking_laws { get; set; }
        public IList<string> timed_modifiers { get; set; }
        public IList<string> unlocking_religions { get; set; }
        public IList<string> disallowing_religions { get; set; }
        public Modifiers building_modifiers { get; set; }
        public Modifiers country_modifiers { get; set; }
        public Modifiers state_modifiers { get; set; }

        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "texture": texture = parser.ReadString(); break;
                case "is_default": is_default = parser.ReadString(); break;
                case "ai_value": ai_value = parser.ReadFloat(); break;
                case "pollution_generation": pollution_generation = parser.ReadInt32(); break;
                case "unlocking_technologies": unlocking_technologies = parser.ReadStringList(); break;
                case "timed_modifiers": timed_modifiers = parser.ReadStringList(); break;
                case "unlocking_production_methods": unlocking_production_methods = parser.ReadStringList(); break;
                case "disallowing_laws": disallowing_laws = parser.ReadStringList(); break;
                case "unlocking_laws": unlocking_laws = parser.ReadStringList(); break;
                case "unlocking_religions": unlocking_religions = parser.ReadStringList(); break;
                case "disallowing_religions": disallowing_religions = parser.ReadStringList(); break;
                case "building_modifiers": building_modifiers = parser.Parse(new Modifiers()); break;
                case "country_modifiers": country_modifiers = parser.Parse(new Modifiers()); break;
                case "state_modifiers": state_modifiers = parser.Parse(new Modifiers()); break;
                default:
                    Console.WriteLine("unknow ProductionMethod prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (texture != null) writer.WriteLine("texture", texture, ValueWrite.Quoted);
            if (is_default != null) writer.WriteLine("is_default", texture);
            if (ai_value != null) writer.WriteLine("ai_value", ai_value.Value);
            if (pollution_generation != null) writer.WriteLine("pollution_generation", pollution_generation.Value);
            if (unlocking_technologies != null)
            {
                writer.Write("unlocking_technologies={ ");
                foreach (var val in unlocking_technologies)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (unlocking_production_methods != null)
            {
                writer.Write("unlocking_production_methods={ ");
                foreach (var val in unlocking_production_methods)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (disallowing_laws != null)
            {
                writer.Write("disallowing_laws={ ");
                foreach (var val in disallowing_laws)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (unlocking_laws != null)
            {
                writer.Write("unlocking_laws={ ");
                foreach (var val in unlocking_laws)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (unlocking_religions != null)
            {
                writer.Write("unlocking_religions={ ");
                foreach (var val in unlocking_religions)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (disallowing_religions != null)
            {
                writer.Write("disallowing_religions={ ");
                foreach (var val in disallowing_religions)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (timed_modifiers != null)
            {
                writer.Write("timed_modifiers={ ");
                foreach (var val in timed_modifiers)
                {
                    writer.Write(val);
                    writer.Write(" ");
                }
                writer.WriteLine("}");
            }
            if (building_modifiers != null) writer.Write("building_modifiers", building_modifiers);
            if (country_modifiers != null) writer.Write("country_modifiers", country_modifiers);
            if (state_modifiers != null) writer.Write("state_modifiers", state_modifiers);
        }
    }
 
    public class Scaled : IParadoxRead, IParadoxWrite
    {
        public Dictionary<string, int> DicFields = new Dictionary<string, int>();
        public void TokenCallback(ParadoxParser parser, string token)
        {
            DicFields.Add(token, parser.ReadInt32());
        }

        public void Write(ParadoxStreamWriter writer)
        {
            foreach (var item in DicFields)
            {
                writer.WriteLine(item.Key, item.Value);
            }
        }
    }
    public class Modifiers : IParadoxRead, IParadoxWrite
    {
        public Scaled workforce_scaled { get; set; }
        public Scaled level_scaled { get; set; }
        public Scaled unscaled { get; set; }
        public void TokenCallback(ParadoxParser parser, string token)
        {
            switch (token)
            {
                case "workforce_scaled": workforce_scaled = parser.Parse(new Scaled()); break;
                case "level_scaled": level_scaled = parser.Parse(new Scaled()); break;
                case "unscaled": unscaled = parser.Parse(new Scaled()); break;
                default:
                    Console.WriteLine("unknow Modifiers prop:  " + token);
                    break;
            }
        }

        public void Write(ParadoxStreamWriter writer)
        {
            if (workforce_scaled != null) writer.Write("workforce_scaled", workforce_scaled);
            if (level_scaled != null) writer.Write("level_scaled", level_scaled);
            if (unscaled != null) writer.Write("unscaled", unscaled);
        }
    }
}
